<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/1
 * Time: 22:19
 */

namespace app\library\traits;


trait ResponceJson
{
    /**
     * 当api接口出现问题时抛出的异常状态
     * @param $code
     * @param $message
     * @param array $data
     * @return string
     */
    public function jsonErr($code,$data=[])
    {
        return $this->jsonResponse($code,$this->showRegError($code),$data);
    }

    /**
     * api接口成功时的返回
     * @param array $data
     * @return string
     */
    public function jsonSuccess($data = [])
    {
        return $this->jsonResponse(0,'Success',$data);
    }
    /**
     * 返回一个json的提示
     * @param $code
     * @param $message
     * @param $data
     * @return string
     */
    private function jsonResponse($code,$message,$data)
    {
        $content = [
            'code'  =>$code,
            'msg'   =>$message,
            'data'  =>$data
        ];

        return $content;
    }

    private function showRegError($code = 0){
        switch ($code) {
            case 1:  $error = 'token为空!';  break;
            case 2:  $error = 'token错误!';  break;
            case 3:  $error = 'token非法!';  break;
            case 4:  $error = 'token过期!';  break;
            case 5:  $error = '参数错误!';  break;
            case 6:  $error = '用户名或密码错误!';  break;

            case 10001: $error = "用户名必须"; break;
            case 10002: $error = "用户名最多不能超过20个字符"; break;
            case 10003: $error = "用户名最少不能低于5个字符"; break;
            case 10004: $error = "密码必须"; break;
            case 10005: $error = "验证码不能为空"; break;
            case 10006: $error = "验证码错误"; break;

            case 50001: $error = ""; break;

            default:  $error = '未知错误';
        }
        return $error;
    }
}